#include <QComboBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QRadioButton>
#include "ItemEnum.h"

int ItemEnum::get()
{
    return m_data;
}

void ItemEnum::set(const int &value)
{
    m_data = value;
    emit itemEdited();
}

QMetaEnum ItemEnum::getMetaEnum()
{
    return m_enum;
}

void ItemEnum::setMetaEnum(const QMetaEnum &e)
{
    m_enum = e;
}

QWidget *ItemEnum::createWidget(WidgetStyle s)
{
    QWidget *w = nullptr;


    if (s == WidgetStyle::ComboBox)
        w = createComboBox();
    else if (s == WidgetStyle::RadioButton)
        w = createRadioButtonGroup();
    else if (s == WidgetStyle::Default)
        w = createWidget(m_widgetStyle == Default ? ComboBox : m_widgetStyle);


    if (w && !w->property("style").isValid())
    {
        w->setProperty("style", s);
    }

    return w;
}

void ItemEnum::update(QWidget *widget, UpdateOption option)
{
    if (widget)
    {
        WidgetStyle style = static_cast<WidgetStyle>(widget->property("style").value<int>());
        if (style == WidgetStyle::ComboBox)
            updateComboBox(widget, option);
        else if (style == WidgetStyle::RadioButton)
            updateRadioButtonGroup(widget, option);
    }
}

void ItemEnum::writeToData(QDataStream &stream) const
{
    stream << m_data;
}

void ItemEnum::readFromData(QDataStream &stream)
{
    stream >> m_data;
    emit itemEdited();
}

QWidget *ItemEnum::createRadioButtonGroup()
{
    auto w = new QWidget;
    auto layout = new QHBoxLayout(w);
    for (int i = 0; i < m_enum.keyCount(); i++)
    {
        auto r = new QRadioButton;
        r->setObjectName(m_enum.valueToKey(m_enum.value(i)));
        r->setText(tr(m_enum.valueToKey(m_enum.value(i))));
        layout->addWidget(r);
        connect(r, &QRadioButton::clicked, this, &ItemEnum::onWidgetEditedWrapper);
    }
    return w;
}

QWidget *ItemEnum::createComboBox()
{
    auto w = new QComboBox;
    for (int i = 0; i < m_enum.keyCount(); i++)
    {
        w->addItem(tr(m_enum.valueToKey(m_enum.value(i))), m_enum.value(i));
        connect(w, &QComboBox::currentTextChanged, this, &ItemEnum::onWidgetEditedWrapper);
    }
    return w;
}

void ItemEnum::updateRadioButtonGroup(QWidget *widget, UpdateOption option)
{
    if (widget != nullptr)
    {
        if (option == UpdateOption::WidgetContent)
        {
            QString name = m_enum.valueToKey(m_data);
            QRadioButton *r = widget->findChild<QRadioButton*>(name, Qt::FindDirectChildrenOnly);
            if (r != nullptr)
            {
                r->setChecked(true);
            }
        }
        else if (option == UpdateOption::Item)
        {
            QObjectList objects = widget->children();
            for (auto obj : objects)
            {
                QRadioButton *r = qobject_cast<QRadioButton*>(obj);
                if (r != nullptr && r->isChecked())
                {
                    m_data = m_enum.keyToValue(r->objectName().toUtf8().data());
                    break;
                }
            }
        }
    }
    else
    {
        qWarning() << "";
    }
}

void ItemEnum::updateComboBox(QWidget *widget, UpdateOption option)
{
    auto w = qobject_cast<QComboBox*>(widget);
    if (w)
    {
        if (option == UpdateOption::WidgetContent)
        {
            w->setCurrentText(m_enum.valueToKey(m_data));
        }
        else if (option == UpdateOption::Item)
        {
            m_data = w->itemData(w->currentIndex()).value<int>();
        }
    }
}
